# -*- coding: utf-8 -*-
""" 
    twExtJS - ToscaWidgets wrapper for ExtJS (see http://extjs.com)
    
    Distributed under the terms of Lesser GNU Public License.
    
    (c) 2007 - Ricardo Girardi Sixel 
"""

from mako.filters import xml_escape
from toscawidgets.core import Widget

def attrs(context, args=None, attrs=None):
    args = args or []
    if not isinstance(args, list):
        args = args.items()
    if attrs:
        args.extend(attrs.items())

    return u" ".join([u'%s:"%s"' % (k,xml_escape(unicode(v)))
                     for k,v in args if v is not None])


class MixedCollection:
    
    collection = []
    
    @property
    def widgets(self):
        for item in self.collection:
            if (item is str ):
                pass
            
class BaseWidget(Widget):
    template = "mako:toscawidgets.widgets.extJS.templates.basewidget"
    renderTo=''
    handlers = []
    params=['params','handlers','obj']
    
    def __init__(self, id=None, parent=None, children=[], **kw):
        self.obj = self
        
            
class Observable(BaseWidget):
    """ ExtJS Widgets base class """    
    __listeners = {}
    
    
    params=BaseWidget.params | frozenset(['componentId'])
    
    def addListener(self,eventName,handler):
        listeners = self.__listeners[eventName]
        
        if listeners==None:
            listeners = []
            self.__listeners[eventName] = listeners
            
        listeners.append(handler)    
        
        
    def listeners(self,eventName):
        return self.__listeners[eventName]
